package com.cskaoyan.service;

import com.cskaoyan.bean.pojo.MallSystem;
import com.cskaoyan.bean.pojo.MallSystemExample;
import com.cskaoyan.bean.vo.ExpressConfigVo;
import com.cskaoyan.bean.vo.MallConfigVo;
import com.cskaoyan.bean.vo.OrderConfigVo;
import com.cskaoyan.bean.wxvo.WxConfigVo;
import com.cskaoyan.mapper.MallSystemMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class ConfigManagementServiceImpl implements ConfigManagementService {

    @Autowired
    MallSystemMapper mallSystemMapper;

    @Autowired
    ConfigManagementServiceImpl configManagementService;

    /**
     * 商场配置回显
     */
    @Override
    public MallConfigVo getConfig() {

        MallSystemExample mallSystemExample = new MallSystemExample ();
        List<MallSystem> mallSystems = mallSystemMapper.selectByExample (mallSystemExample);
        MallConfigVo mallConfigVo = new MallConfigVo ();
        for (MallSystem mallSystem : mallSystems) {
            if ("cskaoyan_mall_mall_name".equals (mallSystem.getKeyName ())) {
                mallConfigVo.setCskaoyanmall_mall_name (mallSystem.getKeyValue ());
            }
            if ("cskaoyan_mall_mall_address".equals (mallSystem.getKeyName ())) {
                mallConfigVo.setCskaoyanmall_mall_address (mallSystem.getKeyValue ());
            }
            if ("cskaoyan_mall_mall_phone".equals (mallSystem.getKeyName ())) {
                mallConfigVo.setCskaoyanmall_mall_phone (mallSystem.getKeyValue ());
            }
            if ("cskaoyan_mall_mall_qq".equals (mallSystem.getKeyName ())) {
                mallConfigVo.setCskaoyanmall_mall_qq (mallSystem.getKeyValue ());
            }
        }
        return mallConfigVo;
    }

    /**
     * cskaoyanmall_mall_name : 商场名称
     * cskaoyanmall_mall_address : 商场地址
     * cskaoyanmall_mall_phone : 商场电话
     * cskaoyanmall_mall_qq : 联系qq
     */

    /**
     * 商场配置修改
     *
     * @param map
     * @return
     */
    @Override
    public int setConfig(Map<String, String> map) {
        String name = map.get ("cskaoyanmall_mall_name");
        String address = map.get ("cskaoyanmall_mall_address");
        String phone = map.get ("cskaoyanmall_mall_phone");
        String qq = map.get ("cskaoyanmall_mall_qq");

        int i1 = configManagementService.updateTargetKeyValue (name, "cskaoyan_mall_mall_name");
        int i2 = configManagementService.updateTargetKeyValue (address, "cskaoyan_mall_mall_address");
        int i3 = configManagementService.updateTargetKeyValue (phone, "cskaoyan_mall_mall_phone");
        int i4 = configManagementService.updateTargetKeyValue (qq, "cskaoyan_mall_mall_qq");

        int affectRows = i1 + i2 + i3 + i4;

        return affectRows;
    }

    /**
     * 运费配置回显
     *
     * @return
     */
    @Override
    public ExpressConfigVo getExpressConfig() {
        MallSystemExample mallSystemExample = new MallSystemExample ();
        List<MallSystem> mallSystems = mallSystemMapper.selectByExample (mallSystemExample);
        ExpressConfigVo expressConfigVo = new ExpressConfigVo ();
        for (MallSystem mallSystem : mallSystems) {
            if ("cskaoyan_mall_express_freight_min".equals (mallSystem.getKeyName ())) {
                String keyValue1 = mallSystem.getKeyValue ();
                expressConfigVo.setCskaoyanmall_express_freight_min (keyValue1);
            }
            if ("cskaoyan_mall_express_freight_value".equals (mallSystem.getKeyName ())) {
                String keyValue2 = mallSystem.getKeyValue ();
                expressConfigVo.setCskaoyanmall_express_freight_value (keyValue2);
            }
        }
        return expressConfigVo;
    }

    /**
     * cskaoyanmall_express_freight_min : 运费满减所需最低消费
     * cskaoyanmall_express_freight_value : 运费满减不足所需运费
     */
    /**
     * 运费配置修改
     *
     * @param map
     * @return
     */
    @Override
    public int setExpressConfig(Map<String, String> map) {

        String freightMin = map.get ("cskaoyanmall_express_freight_min");
        String freightValue = map.get ("cskaoyanmall_express_freight_value");

        int i = configManagementService.updateTargetKeyValue (freightMin, "cskaoyan_mall_express_freight_min");
        int j = configManagementService.updateTargetKeyValue (freightValue, "cskaoyan_mall_express_freight_value");

        int affectRows = i + j;

        return affectRows;
    }

    /**
     * 订单配置回显
     *
     * @return
     */
    @Override
    public OrderConfigVo getOrderConfig() {
        OrderConfigVo orderConfigVo = new OrderConfigVo ();
        MallSystemExample mallSystemExample = new MallSystemExample ();
        List<MallSystem> mallSystems = mallSystemMapper.selectByExample (mallSystemExample);
        for (MallSystem mallSystem : mallSystems) {
            if (mallSystem.getKeyName ().equals ("cskaoyan_mall_order_unpaid")) {
                String keyValue1 = mallSystem.getKeyValue ();
                orderConfigVo.setCskaoyanmall_order_unpaid (keyValue1);
            }
            if (mallSystem.getKeyName ().equals ("cskaoyan_mall_order_unconfirm")) {
                String keyValue2 = mallSystem.getKeyValue ();
                orderConfigVo.setCskaoyanmall_order_unconfirm (keyValue2);
            }
            if (mallSystem.getKeyName ().equals ("cskaoyan_mall_order_comment")) {
                String keyValue3 = mallSystem.getKeyValue ();
                orderConfigVo.setCskaoyanmall_order_comment (keyValue3);
            }
        }
        return orderConfigVo;
    }

    /**
     * cskaoyanmall_order_unpaid : x分钟未付款取消订单
     * cskaoyanmall_order_unconfirm : x天自动确认收货
     * cskaoyanmall_order_comment : x天未评价取消评价资格
     */
    /**
     * 订单配置修改
     *
     * @param map
     * @return
     */
    @Override
    public int setOrderConfig(Map<String, String> map) {
        String orderUnpaid = map.get ("cskaoyanmall_order_unpaid");
        String orderUnconfirm = map.get ("cskaoyanmall_order_unconfirm");
        String orderComment = map.get ("cskaoyanmall_order_comment");

        int i1 = configManagementService.updateTargetKeyValue (orderUnpaid, "cskaoyan_mall_order_unpaid");
        int i2 = configManagementService.updateTargetKeyValue (orderUnconfirm, "cskaoyan_mall_order_unconfirm");
        int i3 = configManagementService.updateTargetKeyValue (orderComment, "cskaoyan_mall_order_comment");

        int affectRows = i1 + i2 + i3;

        return affectRows;
    }

    /**
     * 微信小程序配置回显
     *
     * @return
     */
    @Override
    public WxConfigVo getWxConfig() {
        WxConfigVo wxConfigVo = new WxConfigVo ();
        MallSystemExample mallSystemExample = new MallSystemExample ();
        List<MallSystem> mallSystems = mallSystemMapper.selectByExample (mallSystemExample);
        for (MallSystem system : mallSystems) {
            if ("cskaoyan_mall_wx_share".equals (system.getKeyName ())) {
                wxConfigVo.setCskaoyanmall_wx_share (system.getKeyValue ());
            }
            if ("cskaoyan_mall_wx_index_brand".equals (system.getKeyName ())) {
                wxConfigVo.setCskaoyanmall_wx_index_brand (system.getKeyValue ());
            }
            if ("cskaoyan_mall_wx_index_topic".equals (system.getKeyName ())) {
                wxConfigVo.setCskaoyanmall_wx_index_topic (system.getKeyValue ());
            }
            if ("cskaoyan_mall_wx_index_hot".equals (system.getKeyName ())) {
                wxConfigVo.setCskaoyanmall_wx_index_hot (system.getKeyValue ());
            }
            if ("cskaoyan_mall_wx_catlog_goods".equals (system.getKeyName ())) {
                wxConfigVo.setCskaoyanmall_wx_catlog_goods (system.getKeyValue ());
            }
            if ("cskaoyan_mall_wx_catlog_list".equals (system.getKeyName ())) {
                wxConfigVo.setCskaoyanmall_wx_catlog_list (system.getKeyValue ());
            }
            if ("cskaoyan_mall_wx_index_new".equals (system.getKeyName ())) {
                wxConfigVo.setCskaoyanmall_wx_index_new (system.getKeyValue ());
            }
        }
        return wxConfigVo;
    }


    /**
     * cskaoyanmall_wx_share : 是否允许分享
     * cskaoyanmall_wx_index_brand : 品牌制造商直供栏目品牌商显示数量
     * cskaoyanmall_wx_index_topic : 专题精选栏目显示数量
     * cskaoyanmall_wx_index_hot : 人气推荐栏目商品显示数量
     * cskaoyanmall_wx_catlog_goods : 分类栏目商品显示数量
     * cskaoyanmall_wx_catlog_list : 分类栏目显示数量
     * cskaoyanmall_wx_index_new : 新品首发栏目商品显示数量
     */
    /**
     * 微信小程序配置修改
     *
     * @param map
     * @return
     */
    @Override
    public int setWxConfig(Map<String, String> map) {
        String wxShare = map.get ("cskaoyanmall_wx_share");
        String wxIndexBrand = map.get ("cskaoyanmall_wx_index_brand");
        String wxIndexTopic = map.get ("cskaoyanmall_wx_index_topic");
        String wxIndexHot = map.get ("cskaoyanmall_wx_index_hot");
        String wxCatlogGoods = map.get ("cskaoyanmall_wx_catlog_goods");
        String wxCatlogList = map.get ("cskaoyanmall_wx_catlog_list");
        String wxIndexNew = map.get ("cskaoyanmall_wx_index_new");

        int i = configManagementService.updateTargetKeyValue (wxShare, "cskaoyan_mall_wx_share");
        int i1 = configManagementService.updateTargetKeyValue (wxIndexBrand, "cskaoyan_mall_wx_index_brand");
        int i2 = configManagementService.updateTargetKeyValue (wxIndexTopic, "cskaoyan_mall_wx_index_topic");
        int i3 = configManagementService.updateTargetKeyValue (wxIndexHot, "cskaoyan_mall_wx_index_hot");
        int i4 = configManagementService.updateTargetKeyValue (wxCatlogGoods, "cskaoyan_mall_wx_catlog_goods");
        int i5 = configManagementService.updateTargetKeyValue (wxCatlogList, "cskaoyan_mall_wx_catlog_list");
        int i6 = configManagementService.updateTargetKeyValue (wxIndexNew, "cskaoyan_mall_wx_index_new");

        int affectRows = i + i1 + i2 + i3 + i4 + i5 + i6;
        return affectRows;
    }

    private int updateTargetKeyValue(String update, String targetKeyName) {
        MallSystem mallSystem = new MallSystem ();
        MallSystemExample mallSystemExample = new MallSystemExample ();
        mallSystemExample.or ().andKeyNameEqualTo (targetKeyName).andKeyValueNotEqualTo (update);
        mallSystem.setKeyValue (update)
                .setUpdateTime (new Date ());
        int i = mallSystemMapper.updateByExampleSelective (mallSystem, mallSystemExample);
        return i;
    }
}
